package com.example.es2helloworldpl4;

import java.util.Random;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

	private static final Random gerador = new Random();
	private Button btnGuess;
	private EditText txtGuess;
	
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        final int valor = geraValorAleatorio(1, 10);
        
        btnGuess = (Button) findViewById(R.id.btnGuess);
        txtGuess = (EditText) findViewById(R.id.txtGuess);
        
        btnGuess.setOnClickListener(new onClickListener(){
        	
        	@Override
        	public void onClick(View v) {
        		if (txtGuess.getText() == null ||
        				txtGuess.getText().toString().trim().isEmpty())
        			return;
        		int guess = Integer.parseInt(txtGuess.getText().toString());
        		if (guess == valor)
        			Toast.makeText(getAplicationContext(), "Acertou!", Toast.LENGTH_SHORT).show();
        		else if (guess < valor)
        			Toast.makeText(getAplicationContext(), "Para cima...", Toast.LENGTH_SHORT).show();
        		else
        			Toast.makeText(getAplicationContext(), "para baixo", Toast.LENGTH_SHORT).show();
        		
        		txtGuess.setText("");
        	}
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    private int geraValorAleatorio(int min, int max){
    	int valor = gerador.nextInt(max - min +1);
    	valor = min + valor;
    	return valor;
    }
    
}
